package com.bentley.kotlindemo.ui.kits.home

import android.util.SparseArray
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bentley.kotlindemo.R

class MyHolder(itemView: View, click: OnItemClick) : RecyclerView.ViewHolder(itemView),
    View.OnClickListener {

    var textView: TextView

    private var click: OnItemClick? = null

    init {
        textView = itemView.findViewOften(R.id.tv_test)
        this.click = click

        itemView.setOnClickListener(this)
    }

    override fun onClick(p0: View?) {
        if (p0 != null) {
            click!!.onItemClick(p0, adapterPosition)
        }
    }

    fun <T : View> View.findViewOften(viewId: Int): T {
        var viewHolder: SparseArray<View> = tag as? SparseArray<View> ?: SparseArray()
        tag = viewHolder
        var childView: View? = viewHolder.get(viewId)
        if (null == childView) {
            childView = findViewById(viewId)
            viewHolder.put(viewId, childView)
        }
        return childView as T
    }




}